<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<% String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <base href="<%=basePath%>">
    <meta charset="UTF-8">
    <link href="jquery/bootstrap_3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet"/>
    <script type="text/javascript" src="jquery/jquery-1.11.1-min.js"></script>
    <script type="text/javascript" src="jquery/bootstrap_3.3.0/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="jquery/bs_pagination/jquery.bs_pagination.min.css">
    <script type="text/javascript" src="jquery/bs_pagination/jquery.bs_pagination.min.js"></script>
    <script type="text/javascript" src="jquery/bs_pagination/en.js"></script>
    <script type="text/javascript" src="common/js/common.js"></script>

    <script type="text/javascript">

        $(function () {
            // 隐藏提示框域
            $("#alert-success").hide();
            $("#alert-info").hide();
            $("#alert-warning").hide();
            $("#alert-danger").hide();
            getClueList(1, 3);
            // 打开创建线索模态框
            $("#createBtn").click(function () {
                getUserList();
                $("#createClueModal").modal("show");
            });
            // 保存一个线索
            $("#saveBtn").click(function () {
                $.ajax({
                    url: "workbench/clue/saveClue.do",
                    data: {
                        "fullname": $("#create-name").val(),
                        "appellation": $("#create-call").val(),
                        "owner": $("#create-clueOwner").val(),
                        "company": $("#create-company").val(),
                        "job": $("#create-job").val(),
                        "email": $("#create-email").val(),
                        "phone": $("#create-phone").val(),
                        "website": $("#create-website").val(),
                        "mphone": $("#create-mphone").val(),
                        "state": $("#create-status").val(),
                        "source": $("#create-source").val(),
                        "description": $("#create-describe").val(),
                        "contactSummary": $("#create-contactSummary").val(),
                        "nextContactTime": $("#create-nextContactTime").val(),
                        "address": $("#create-address").val()
                    },
                    type: "post",
                    datatype: "json",
                    success: function (data) {
                        if (data) {
                            globalAlert(alert_success, "保存成功")
                        } else {
                            globalAlert(alert_danger, "保存失败")
                        }
                        getClueList(1, $("#clue-page").bs_pagination('getOption', 'rowsPerPage'))
                        $("#createClueModal").modal("hide")
                        // 清空文本框中的值
                        $(".createInput").val("")
                    },
                    error: function () {
                        globalAlert(alert_danger, "保存失败")
                    }
                });
            });

            // 全选、反选操作
            $("#checkAll").click(function () {
                $("input[name=checkbox]").prop("checked", this.checked);
            });
            $("#clueList").on("click", $("input[name=checkbox]"), function () {
                // 判断已经选择的框的数量是否和所有选择框的数量一致，如果一致，则代表全选，否则不全选
                $("#checkAll").prop("checked", $("input[name=checkbox]").length === $("input[name=checkbox]:checked").length);
            });
        });

        // 获取线索列表
        function getClueList(pageNo, pageSize) {
            // 每次加载都清空全选框的选中状态
            $("#checkAll").prop("checked", false);

            $("#search-owner").val($.trim($("#hidden-search-owner").val()));
            $("#search-fullname").val($.trim($("#hidden-search-fullname").val()));
            $("#search-company").val($.trim($("#hidden-search-company").val()));
            $("#search-mphone").val($.trim($("#hidden-search-mphone").val()));
            $("#search-phone").val($.trim($("#hidden-search-phone").val()));
            $("#search-source").val($.trim($("#hidden-search-source").val()));
            $("#search-state").val($.trim($("#hidden-search-state").val()));
            let html = "";
            $.ajax({
                url: "workbench/clue/getClueList.do",
                data: {
                    "owner": $("#search-owner").val(),
                    "fullname": $("#search-fullname").val(),
                    "company": $("#search-company").val(),
                    "mphone": $("#search-mphone").val(),
                    "phone": $("#search-phone").val(),
                    "source": $("#search-source").val(),
                    "state": $("#search-state").val(),
                    "pageNo": pageNo,
                    "pageSize": pageSize
                },
                type: "get",
                dataType: "json",
                success: function (data) {
                    let html = "";
                    $.each(data.dataList, function (index, item) {
                        html += '<tr><td><input type="checkbox" name="checkbox"/></td>';
                        html += '<td><a onclick="window.location.href=\'workbench/clue/findClueById.do?id=' + item.id + '\';" style="text-decoration: none; cursor: pointer;">' + item.fullname + '</a></td>';
                        html += '<td>' + item.company + '</td>';
                        html += '<td>' + item.phone + '</td>';
                        html += '<td>' + item.mphone + '</td>';
                        html += '<td>' + item.source + '</td>';
                        html += '<td>' + item.owner + '</td>';
                        html += '<td>' + item.source + '</td></tr>';
                    });
                    $("#clueList").html(html);

                    let totalPages = Math.ceil(data.total / pageSize);

                    $("#clue-page").bs_pagination({
                        currentPage: pageNo,
                        rowsPerPage: pageSize,
                        maxRowsPerPage: 20,   // 每页最多显示的记录条数
                        totalPages: totalPages, // 总页数
                        totalRows: data.total,  // 总记录数
                        visiblePageLinks: 3, // 显示几个卡片
                        showGoToPage: true,
                        showRowsPerPage: true,
                        showRowsInfo: true,
                        showRowsDefaultInfo: true,
                        onChangePage: function (event, data) {
                            getClueList(data.currentPage, data.rowsPerPage);
                        }
                    });
                },
                error: function () {
                    globalAlert(alert_info, "获取线索列表失败");
                }
            });
        }

        // 获取用户名称
        function getUserList() {
            let html = "<option disabled selected>------请选择------</option>"
            $.ajax({
                url: "settings/user/getUserList.do",
                type: "get",
                dataType: "json",
                success: function (data) {
                    /*遍历的每一个元素就是一个user*/
                    $.each(data, function (index, item) {
                        html += "<option>" + item.name + "</option>";
                        $("#create-clueOwner").html(html);
                        $("#edit-clueOwner").html(html);
                    });
                },
                error: function () {
                    html += "<option disabled>获取用户列表失败 </option>"
                }
            });

            // 查询线索
            $("#searchBtn").click(function () {
                // 用隐藏域保存搜索条件
                $("#hidden-search-owner").val($.trim($("#search-owner").val()));
                $("#hidden-search-state").val($.trim($("#search-state").val()));
                $("#hidden-search-source").val($.trim($("#search-source").val()));
                $("#hidden-search-fullname").val($.trim($("#search-fullname").val()));
                $("#hidden-search-mphone").val($.trim($("#search-mphone").val()));
                $("#hidden-search-phone").val($.trim($("#search-phone").val()));
                $("#hidden-search-company").val($.trim($("#search-company").val()));

                getClueList(1, $("#clue-page").bs_pagination('getOption', 'rowsPerPage'));
            });
        }

    </script>
</head>
<body>
<%-- 创建全局提示框 --%>
<div class="globalAlert"
     style="position: absolute;top:150px;left: 50%;width: 25%; z-index: 1000;text-align: center;vertical-align: center">
    <div class="alert alert-success" role="alert" style="height: 50px;" id="alert-success"></div>
    <div class="alert alert-info" role="alert" style="height: 50px;" id="alert-info"></div>
    <div class="alert alert-warning" role="alert" style="height: 50px;" id="alert-warning"></div>
    <div class="alert alert-danger" role="alert" style="height: 50px;" id="alert-danger"></div>
</div>

<!-- 创建线索的模态窗口 -->
<div class="modal fade" id="createClueModal" role="dialog">
    <div class="modal-dialog" role="document" style="width: 90%;">
        <div class="modal-content">
            <div class="modal-header">
                <button class="close" data-dismiss="modal" type="button">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">创建线索</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form">

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="create-clueOwner">所有者<span
                                style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select class="form-control" id="create-clueOwner">
                            </select>
                        </div>
                        <label class="col-sm-2 control-label" for="create-company">公司<span
                                style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input class="form-control createInput" id="create-company" type="text">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="create-call">称呼</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select class="form-control" id="create-call">
                                <option disabled selected>------请选择------</option>
                                <c:forEach items="${appellation}" var="a">
                                    <option value="${a.value}">${a.text}</option>
                                </c:forEach>
                            </select>
                        </div>
                        <label class="col-sm-2 control-label" for="create-name">姓名<span
                                style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input class="form-control createInput" id="create-name" type="text">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="create-job">职位</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input class="form-control createInput" id="create-job" type="text">
                        </div>
                        <label class="col-sm-2 control-label" for="create-email">邮箱</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input class="form-control createInput" id="create-email" type="text">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="create-phone">公司座机</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input class="form-control createInput" id="create-phone" type="text">
                        </div>
                        <label class="col-sm-2 control-label" for="create-website">公司网站</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input class="form-control createInput" id="create-website" type="text">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="create-mphone">手机</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input class="form-control createInput" id="create-mphone" type="text">
                        </div>
                        <label class="col-sm-2 control-label" for="create-status">线索状态</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select class="form-control" id="create-status">
                                <option disabled selected>------请选择------</option>
                                <c:forEach items="${clueState}" var="c">
                                    <option value="${c.value}">${c.text}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="create-source">线索来源</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select class="form-control" id="create-source">
                                <option disabled selected>------请选择------</option>
                                <c:forEach items="${source}" var="s">
                                    <option value="${s.value}">${s.text}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="create-describe">线索描述</label>
                        <div class="col-sm-10" style="width: 81%;">
                            <textarea class="form-control createInput" id="create-describe" rows="3"
                                      style="resize: none"></textarea>
                        </div>
                    </div>

                    <div style="position: relative;top: 15px;">
                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="create-contactSummary">联系纪要</label>
                            <div class="col-sm-10" style="width: 81%;">
                                <textarea class="form-control createInput" id="create-contactSummary" rows="3"
                                          style="resize: none"></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="create-nextContactTime">下次联系时间</label>
                            <div class="col-sm-10" style="width: 300px;">
                                <input class="form-control createInput" id="create-nextContactTime" type="date">
                            </div>
                        </div>
                    </div>

                    <div style="position: relative;top: 20px;">
                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="create-address">详细地址</label>
                            <div class="col-sm-10" style="width: 81%;">
                                <textarea class="form-control createInput" id="create-address" rows="1"
                                          style="resize: none"></textarea>
                            </div>
                        </div>
                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal" type="button">关闭</button>
                <button class="btn btn-primary" id="saveBtn" type="button">保存</button>
            </div>
        </div>
    </div>
</div>

<!-- 修改线索的模态窗口 -->
<div class="modal fade" id="editClueModal" role="dialog">
    <div class="modal-dialog" role="document" style="width: 90%;">
        <div class="modal-content">
            <div class="modal-header">
                <button class="close" data-dismiss="modal" type="button">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title">修改线索</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form">

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="edit-clueOwner">所有者<span
                                style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select class="form-control" id="edit-clueOwner">
                            </select>
                        </div>
                        <label class="col-sm-2 control-label" for="edit-company">公司<span
                                style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input class="form-control" id="edit-company" type="text" value="动力节点">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="edit-call">称呼</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select class="form-control" id="edit-call">
                                <option disabled selected>------请选择------</option>
                                <c:forEach items="${appellation}" var="a">
                                    <option value="${a.value}">${a.text}</option>
                                </c:forEach>
                            </select>
                        </div>
                        <label class="col-sm-2 control-label" for="edit-surname">姓名<span
                                style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input class="form-control" id="edit-surname" type="text" value="李四">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="edit-job">职位</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input class="form-control" id="edit-job" type="text" value="CTO">
                        </div>
                        <label class="col-sm-2 control-label" for="edit-email">邮箱</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input class="form-control" id="edit-email" type="text" value="lisi@bjpowernode.com">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="edit-phone">公司座机</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input class="form-control" id="edit-phone" type="text" value="010-84846003">
                        </div>
                        <label class="col-sm-2 control-label" for="edit-website">公司网站</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input class="form-control" id="edit-website" type="text"
                                   value="http://www.bjpowernode.com">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="edit-mphone">手机</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input class="form-control" id="edit-mphone" type="text" value="12345678901">
                        </div>
                        <label class="col-sm-2 control-label" for="edit-status">线索状态</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select class="form-control" id="edit-status">
                                <option disabled selected>------请选择------</option>
                                <c:forEach items="${clueState}" var="c">
                                    <option value="${c.value}">${c.text}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="edit-source">线索来源</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select class="form-control" id="edit-source">
                                <option disabled selected>------请选择------</option>
                                <c:forEach items="${source}" var="s">
                                    <option value="${a.value}">${a.text}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="edit-describe">描述</label>
                        <div class="col-sm-10" style="width: 81%;">
                            <textarea class="form-control" id="edit-describe" rows="3"
                                      style="resize: none">这是一条线索的描述信息</textarea>
                        </div>
                    </div>

                    <div style="height: 1px; width: 103%; background-color: #D5D5D5; left: -13px; position: relative;"></div>

                    <div style="position: relative;top: 15px;">
                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="edit-contactSummary">联系纪要</label>
                            <div class="col-sm-10" style="width: 81%;">
                                <textarea class="form-control" id="edit-contactSummary" rows="3" style="resize: none">这个线索即将被转换</textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="edit-nextContactTime">下次联系时间</label>
                            <div class="col-sm-10" style="width: 300px;">
                                <input class="form-control" id="edit-nextContactTime" type="text" value="2017-05-01">
                            </div>
                        </div>
                    </div>

                    <div style="height: 1px; width: 103%; background-color: #D5D5D5; left: -13px; position: relative; top : 10px;"></div>

                    <div style="position: relative;top: 20px;">
                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="edit-address">详细地址</label>
                            <div class="col-sm-10" style="width: 81%;">
                                <textarea class="form-control" id="edit-address" rows="1" style="resize: none">北京大兴区大族企业湾</textarea>
                            </div>
                        </div>
                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal" type="button">关闭</button>
                <button class="btn btn-primary" data-dismiss="modal" type="button">更新</button>
            </div>
        </div>
    </div>
</div>


<div>
    <div style="position: relative; left: 10px; top: -10px;">
        <div class="page-header">
            <h3>线索列表</h3>
        </div>
    </div>
</div>

<div style="position: relative; top: -20px; left: 0px; width: 100%; height: 100%;">

    <div style="width: 100%; position: absolute;top: 5px; left: 10px;">

        <div class="btn-toolbar" role="toolbar" style="height: 80px;">
            <form class="form-inline" role="form" style="position: relative;top: 8%; left: 5px;">

                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-addon">名称</div>
                        <input class="form-control" type="text" id="search-fullname"/>
                        <input hidden id="hidden-search-fullname"/>
                    </div>
                </div>

                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-addon">公司</div>
                        <input class="form-control" type="text" id="search-company"/>
                        <input hidden id="hidden-search-company"/>
                    </div>
                </div>

                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-addon">公司座机</div>
                        <input class="form-control" type="text" id="search-phone"/>
                        <input hidden id="hidden-search-phone"/>
                    </div>
                </div>

                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-addon">线索来源</div>
                        <input hidden id="hidden-search-source"/>
                        <select class="form-control" id="search-source">
                            <option disabled selected>------请选择------</option>
                            <c:forEach items="${source}" var="s">
                                <option value="s.value">${s.text}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>

                <br>

                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-addon">所有者</div>
                        <input class="form-control" type="text" id="search-owner"/>
                        <input hidden id="hidden-search-owner"/>
                    </div>
                </div>


                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-addon">手机</div>
                        <input class="form-control" type="text" id="search-mphone"/>
                        <input hidden id="hidden-search-mphone"/>
                    </div>
                </div>

                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-addon">线索状态</div>
                        <input hidden id="hidden-search-state">
                        <select class="form-control" id="search-state">
                            <option disabled selected>------请选择------</option>
                            <c:forEach items="${clueState}" var="c">
                                <option value="c.value">${c.text}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>

                <button class="btn btn-default" type="button" id="searchBtn">查询</button>

            </form>
        </div>
        <div class="btn-toolbar" role="toolbar"
             style="background-color: #F7F7F7; height: 50px; position: relative;top: 40px;">
            <div class="btn-group" style="position: relative; top: 18%;">
                <button class="btn btn-primary" id="createBtn" type="button"><span
                        class="glyphicon glyphicon-plus"></span> 创建
                </button>
                <button class="btn btn-default" data-target="#editClueModal" data-toggle="modal" type="button"><span
                        class="glyphicon glyphicon-pencil"></span> 修改
                </button>
                <button class="btn btn-danger" type="button" id="deleteBtn"><span
                        class="glyphicon glyphicon-minus"></span> 删除
                </button>
            </div>


        </div>
        <div style="position: relative;top: 50px;">
            <table class="table table-hover">
                <thead>
                <tr style="color: #B3B3B3;">
                    <td><input type="checkbox" id="checkAll"/></td>
                    <td>名称</td>
                    <td>公司</td>
                    <td>公司座机</td>
                    <td>手机</td>
                    <td>线索来源</td>
                    <td>所有者</td>
                    <td>线索状态</td>
                </tr>
                </thead>
                <tbody id="clueList">
                </tbody>
            </table>
        </div>

        <div style="height: 50px; position: relative;top: 60px;">
            <div id="clue-page"></div>
        </div>

    </div>

</div>
</body>
</html>